package org.example.diet_compass.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.example.diet_compass.domain.CommentLikes;
import org.example.diet_compass.domain.Comments;
import org.example.diet_compass.dto.CommentsListDto;
import org.example.diet_compass.dto.LikeCommentDto;
import org.example.diet_compass.resp.Result;
import org.example.diet_compass.resp.ResultCodeEnum;
import org.example.diet_compass.service.CommentLikesService;
import org.example.diet_compass.service.CommentsService;
import org.modelmapper.ModelMapper;
import org.modelmapper.convention.NameTokenizers;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;


import java.util.List;

/**
 * @author Archer_hui
 * @version 1.0
 * @Date 2025/3/2 16:38
 */
@Tag(name = "评论控制器", description = "用于处理评分和评论功能的控制器")
@RestController
@RequestMapping("/comments")
public class CommentController {
    @Resource
    CommentsService commentsService;
    @Resource
    CommentLikesService commentLikesService;
    @PostMapping("/add")
    @Operation(summary = "添加评论")
    public Result<Void> addCommentDirect(@RequestBody Comments comments) {
        commentsService.save(comments);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @PostMapping("/like-comment")
    @Transactional
    @Operation(summary = "点赞评论")
    public Result<Void> likeComment(@RequestBody List<LikeCommentDto> likeCommentDtoList ) {
        //前端得来的点赞修改记录作分类
        //1、无记录，点赞了新加,并更新评论表中指定评论的点赞数 +1
        //2、有记录，取消了点赞，删除点赞记录，并让评论表中的点赞数 -1
        //3、无记录，无点赞，有记录，有点赞的都不做处理
        likeCommentDtoList.forEach(likeCommentDto -> {
            int userId = likeCommentDto.getUser_id();
            Long commentId = likeCommentDto.getId();
            int res = commentLikesService.getByUserIdAndCommentId(userId, commentId);
            //无记录，点赞
            if (res == 0 && likeCommentDto.getIs_like()) {
                CommentLikes commentLike = new CommentLikes(null, userId, commentId, null);
                commentLikesService.save(commentLike);
                commentsService.updateLikeNums(commentId,1);
            }else if (res > 0 && !likeCommentDto.getIs_like()) {
                //有记录，取消了点赞
                commentLikesService.removeByUserIdAndCommentId(userId,commentId);
                commentsService.updateLikeNums(commentId,-1);
            }
        });
                return Result.build(null, ResultCodeEnum.SUCCESS);
    }
    @DeleteMapping("/delete")
    @Operation(summary = "删除评论")
    public Result<Void> deleteComment(int userId) {
        commentsService.removeById(userId);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/get")
    @Operation(summary = "获取特定商品的评论列表")
    public Result<List<Comments>> getComments(int goodsId) {
        QueryWrapper<Comments> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goods_id", goodsId);
       return Result.build(commentsService.list(queryWrapper), ResultCodeEnum.SUCCESS   ) ;
    }

    @GetMapping("/get-user-comments")
    @Operation(summary = "获取用户的评论记录")
    public Result<List<Comments>> getUserComments(@RequestParam("userId") int userId) {
        return Result.build(commentsService.getCommentsByUserId(userId), ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/get-goods-comments")
    @Operation(summary = "获取特定菜品的所有的评论")
    public Result<List<CommentsListDto>> getGoodsComments(@RequestParam("goodId") String goodsId) {
        return Result.build(commentsService.getGoodsComments(goodsId), ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/get-good-comment-total")
    @Operation(summary = "用于获取特定商品的评论总数")
    public Result<Integer> getGoodsCommentTotal(@RequestParam("goodId") String goodId) {
        return Result.build(commentsService.getGoodCommentTotal(goodId), ResultCodeEnum.SUCCESS);
    }
    @Transactional
    @DeleteMapping("/delete-good-comment")
    @Operation(summary = "删除商品的某条评论")
    public Result<Void> deleteGoodsComment(@RequestParam("commentId") Integer commentId) {
        //删除当前评论...原来级联删除不用自己写的啊...设置数据库里的外键约束就好了。。。第一次知道
        commentsService.removeByCommentId(commentId);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Transactional
    @PostMapping("/add-good-comment")
    @Operation(summary = "添加特定商品的评论")
    public Result<Long> addGoodsComment(@RequestBody CommentsListDto commentsListDto, @RequestParam("goodId") String goodId) {
        Comments comment = new Comments();
        ModelMapper modelMapper = new ModelMapper();
        //设置匹配规则，让前端的下划线变量映射到类中的驼峰命名
        modelMapper.getConfiguration()
                .setSourceNameTokenizer(NameTokenizers.UNDERSCORE)
                .setDestinationNameTokenizer(NameTokenizers.CAMEL_CASE);
        modelMapper.map(commentsListDto, comment);
        comment.setGoodsId(goodId);
        commentsService.save(comment);
        return Result.build(comment.getCommentId(), ResultCodeEnum.SUCCESS);
    }
}
